package com.easyctrl.db;
  
import java.util.ArrayList;
 
import com.easyctrl.fragment.UserCenterFragment;
import com.easyctrl.fragment.UserSetFragment;
import com.easyctrl.iface.BaseDBImpl; 
import com.easyctrl.ldy.domain.UserScene;

import android.content.ContentValues;
import android.content.Context; 
import android.database.Cursor;

public class UserSceneManager extends DataBaseManage implements BaseDBImpl<UserScene>{

	private static UserSceneManager instance;
	public static UserSceneManager getInstance(Context context){
		if(instance == null){
			instance= new UserSceneManager(context);
		}
		return instance;
	}
	private UserSceneManager(Context context) {
		super(context); 
	}

	public void deleteAll(){
		synchronized (LOCK) {
			openWriteDB();
			database.delete(UserScene.Table.T_NAME, null, null);
			database.execSQL(" DELETE FROM sqlite_sequence ");
		}
	}
	
	public void batchSave(ArrayList<UserScene> userScenes){
		synchronized (LOCK) {
			
			openWriteDB(); 
			UserScene userScene = userScenes.get(0);
			
			Cursor cursor = null;
			try {
				cursor = database.query(UserScene.Table.T_NAME,null, " floorID = ? and roomID = ? ", 
						new String[]{String.valueOf(userScene.floorID),String.valueOf(userScene.roomID)},
						null,null,null);
				
				if(cursor.getCount() > 0) {
					return ;
				}
				
			} catch (Exception e) { 
				e.printStackTrace();
			} finally {
				if(cursor != null) {
					cursor.close();
				}
			} 
			
			database.beginTransaction();
			for (int i = 0; i < userScenes.size(); i++) {
				database.insert(UserScene.Table.T_NAME, null, beanToValues(userScenes.get(i)));
			}
			database.setTransactionSuccessful();
			database.endTransaction();
		}
	}
	
	public ArrayList<UserScene> findByFloorIdAndRoomID(int floor,int room ){
		synchronized (LOCK) {
			openReadDB();
			ArrayList<UserScene> arrayUser = new ArrayList<UserScene>();
			Cursor cursor = null;
			try { 
				
				String rawSql = "select a.userSceneID,a.name,a.pbindID,a.vbindID,a.sbindType,b.keyValue, " +
						" b.bind_moduleID ,c.key from easy_user_scene a " +
						" left join easy_bind b on a.pbindID = b.bindID " +
						" left join easy_virtual_key c on a.vbindID = c.virtualID " +
						" where floorID = ? and roomID = ? ";
				
				cursor = database.rawQuery(rawSql, new String[]{
						String.valueOf(floor),String.valueOf(room)}); 
				while(cursor.moveToNext()) {
					arrayUser.add(cursorToBean(cursor));
				}
			} catch (Exception e) { 
				e.printStackTrace();
			} finally {
				if(cursor != null){
					cursor.close();
				}
			}
			
			return arrayUser;
		}
	}
	
	@Override
	public void add(UserScene t) { 
		
	}

	@Override
	public int delete(int id) { 
		return 0;
	}

	@Override
	public int update(UserScene t, int id) { 
		
		synchronized (LOCK) {
			openWriteDB();
			return database.update(UserScene.Table.T_NAME, updateValues(t), 
					" userSceneID = ? ", new String[]{String.valueOf(t.userSceneID)});
		}
	}
	
	public void updateName(String newName,int id){
		synchronized (LOCK) {
			openWriteDB(); 
			database.execSQL(" update "+UserScene.Table.T_NAME+" set name = ? where userSceneID = ? ", new String[]{
					newName,String.valueOf(id)
			});
		}
	}
	
	private ContentValues updateValues(UserScene t){
		ContentValues values = new ContentValues();
		values.put(UserScene.Table.FIELD_SBINDTYPE, t.sbindType);
		values.put(UserScene.Table.FIELD_VBINDID, t.vbindID);
		values.put(UserScene.Table.FIELD_PBINDID, t.pbindID);
		return values;
	}

	@Override
	public ArrayList<UserScene> findAll() { 
		return null;
	}

	@Override
	public ContentValues beanToValues(UserScene bean) { 
		ContentValues values = new ContentValues();
		values.put(UserScene.Table.FIELD_FLOOR, bean.floorID);
		values.put(UserScene.Table.FIELD_ROOM, bean.roomID); 
		values.put(UserScene.Table.FIELD_SBINDTYPE,-1);
		return values;
	}

	@Override
	public UserScene cursorToBean(Cursor cursor) { 
		UserScene scene = new UserScene();
		scene.userSceneID = cursor.getInt(getColunmName(cursor,UserScene.Table.FIELD_USERSCENEID)); 
		scene.sbindType = cursor.getInt(getColunmName(cursor,UserScene.Table.FIELD_SBINDTYPE)); 
		scene.name = cursor.getString(getColunmName(cursor,UserScene.Table.FIELD_NAME));
		scene.panelID = cursor.getInt(getColunmName(cursor,"bind_moduleID"));
		scene.panelkey = cursor.getInt(getColunmName(cursor,"keyValue"));
		scene.key = cursor.getInt(getColunmName(cursor,"key"));
		scene.pbindID = cursor.getInt(getColunmName(cursor,UserScene.Table.FIELD_PBINDID));
		scene.vbindID = cursor.getInt(getColunmName(cursor,UserScene.Table.FIELD_VBINDID));
		return scene;
	}

	private int getColunmName(Cursor cursor,String colunmName){
		return cursor.getColumnIndexOrThrow(colunmName);
	}
	
	@Override
	public Cursor getCursor(String where, String[] args) {
		return null;
	}
 
}
